package me.feng.component
{
	import me.feng.debug.assert;

	/**
	 *
	 * @author feng 2015-12-4
	 */
	public class ComponentTester
	{

		[Test]
		public function testAddComponent():void
		{
			var container:Component = new Component();
			var com:Component = new Component();
			container.addComponent(com);
			container.addComponent(com);

			assert(container.hasComponent(com));
			assert(container.numComponents == 1);

			var comA:ComponentA = new ComponentA();
			container.addComponent(comA);

			assert(container.numComponents == 2);

			var throwError:Boolean = false;
			try
			{
				container.addComponent(container);
			}
			catch (error:Error)
			{
				throwError = true;
			}
			assert(throwError);
		}

		[Test]
		public function testAddComponentAt():void
		{
			var container:Component = new Component();
			var com:Component = new Component();
			container.addComponentAt(com, 0);

			assert(container.numComponents == 1);

			container.addComponentAt(com, 1);
			assert(container.numComponents == 1);

			var comA:ComponentA = new ComponentA();
			container.addComponentAt(comA, 0);
			assert(container.numComponents == 2);

			var throwError:Boolean = false;
			try
			{
				container.addComponentAt(container, 0);
			}
			catch (error:Error)
			{
				throwError = true;
			}
			assert(throwError);
		}

		[Test]
		public function testGetComponentByClass():void
		{
			var container:Component = new Component();
			var com:Component = new ComponentA();
			var com1:Component = new Component();

			container.addComponent(com);
			container.addComponent(com1);

			var getCom:Component = container.getComponentByClass(Component);
			assert(getCom == com1);
		}

		[Test]
		public function testGetComponentsByClass():void
		{
			var container:Component = new Component();
			var com:Component = new Component();
			var com1:Component = new Component();

			container.addComponent(com);
			container.addComponent(com1);

			var getComs:Array = container.getComponentsByClass(Component);
			assert(getComs.length == 2);

			assert(getComs[0] == com);
			assert(getComs[1] == com1);
		}

		[Test]
		public function getOrCreateComponentByClass():void
		{
			var container:Component = new Component();
			var com:Component = container.getOrCreateComponentByClass(ComponentA);
			assert(com is ComponentA);
		}

		[Test]
		public function testGetComponentByName():void
		{
			var container:Component = new Component();
			var comA:ComponentA = new ComponentA();
			comA.componentName = "sdfjaslk";

			container.addComponent(new ComponentB());
			container.addComponent(comA);

			assert(container.getComponentByName(comA.componentName) == comA);
		}

		[Test]
		public function testGetComponentIndex():void
		{
			var container:Component = new Component();
			container.addComponent(new Component());
			var comA:ComponentA = new ComponentA();
			container.addComponent(comA);

			assert(1 == container.getComponentIndex(comA));

			var throwError:Boolean = false;
			try
			{
				container.getComponentIndex(new Component());
			}
			catch (error:Error)
			{
				throwError = true;
			}
			assert(throwError);
		}

		[Test]
		public function testGetComponentsByName():void
		{
			var container:Component = new Component();

			var compName:String = "sdfskj";
			var com1:Component = new Component();
			com1.componentName = compName;
			var com2:Component = new Component();
			com2.componentName = compName;

			container.addComponent(new Component());
			container.addComponent(com1);
			container.addComponent(com2);

			var comps:Array = container.getComponentsByName(compName);
			assert(comps[0] == com1);
			assert(comps[1] == com2);
		}

		[Test]
		public function testHasComponent():void
		{
			var com:Component = new Component();
			var container:Component = new Component();
			container.addComponent(com);
			assert(container.hasComponent(com));
		}

		[Test]
		public function testGet_numComponents():void
		{
			var num:int = 10 * Math.random() + 2;
			var container:Component = new Component();
			for (var i:int = 0; i < num; i++)
			{
				container.addComponent(new Component());
			}
			assert(container.numComponents == num);
		}

		[Test]
		public function testRemoveComponent():void
		{
			var container:Component = new Component();
			var com:Component = new Component();
			container.addComponent(com);
			container.removeComponent(com);
			assert(container.numComponents == 0);

			var throwError:Boolean = false;
			try
			{
				container.removeComponent(com);
			}
			catch (error:Error)
			{
				throwError = true;
			}
			assert(throwError);
		}

		[Test]
		public function testRemoveComponentAt():void
		{
			var container:Component = new Component();
			var com:Component = new Component();
			var num:int = 10;
			var index:int = Math.random() * num;

			var comA:ComponentA = new ComponentA();
			for (var i:int = 0; i < num; i++)
			{
				if (index == i)
				{
					container.addComponent(comA);
				}
				else
				{
					container.addComponent(new Component());
				}
			}
			index = container.getComponentIndex(comA);
			var comAA:Component = container.removeComponentAt(index);
			assert(container.numComponents == num - 1);
			assert(comAA == comA);
			assert(!container.hasComponent(comA))
		}

		[Test]
		public function testSetComponentIndex():void
		{
			var container:Component = new Component();
			var comA:ComponentA = new ComponentA();
			var comB:ComponentB = new ComponentB();
			container.addComponent(comA);
			container.addComponent(comB);
			assert(container.getComponentAt(0) == comA);
			assert(container.getComponentAt(1) == comB);

			container.setComponentIndex(comA, 1);
			assert(container.getComponentAt(0) == comB);
			assert(container.getComponentAt(1) == comA);
		}

		[Test]
		public function getComponentAt():void
		{
			var container:Component = new Component();
			var comA:ComponentA = new ComponentA();
			var comB:ComponentB = new ComponentB();
			container.addComponent(comA);
			container.addComponent(comB);
			assert(container.getComponentAt(0) == comA);
			assert(container.getComponentAt(1) == comB);
		}

		[Test]
		public function testSwapComponents():void
		{
			var container:Component = new Component();
			var comA:ComponentA = new ComponentA();
			var comB:ComponentB = new ComponentB();
			container.addComponent(comA);
			container.addComponent(new Component());
			container.addComponent(comB);
			assert(container.getComponentAt(0) == comA);
			assert(container.getComponentAt(2) == comB);

			container.swapComponents(comA, comB);
			assert(container.getComponentAt(0) == comB);
			assert(container.getComponentAt(2) == comA);
		}

		[Test]
		public function testSwapComponentsAt():void
		{
			var container:Component = new Component();
			var comA:ComponentA = new ComponentA();
			var comB:ComponentB = new ComponentB();

			container.addComponent(comA);
			container.addComponent(new Component());
			container.addComponent(comB);
			assert(container.getComponentAt(0) == comA);
			assert(container.getComponentAt(2) == comB);

			container.swapComponentsAt(0, 2);
			assert(container.getComponentAt(0) == comB);
			assert(container.getComponentAt(2) == comA);
		}
	}
}
import me.feng.component.Component;
import me.feng.utils.ClassUtils;

class ComponentA extends Component
{
	public function ComponentA()
	{
		componentName = ClassUtils.getDefaultName(this);
	}
}

class ComponentB extends Component
{
	public function ComponentB()
	{
		componentName = ClassUtils.getDefaultName(this);
	}
}
